<template>
  <el-dialog
    :title="!dataForm.diId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="小说id" prop="diBookNameId">
      <el-input v-model="dataForm.diBookNameId" placeholder="小说id"></el-input>
    </el-form-item>
    <el-form-item label="评论内容" prop="diContent">
      <el-input v-model="dataForm.diContent" placeholder="评论内容"></el-input>
    </el-form-item>
    <el-form-item label="父级评论" prop="diParent">
      <el-input v-model="dataForm.diParent" placeholder="父级评论"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="diCreationTime">
      <el-input v-model="dataForm.diCreationTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="diUpdateTime">
      <el-input v-model="dataForm.diUpdateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="评论状态" prop="diStatus">
      <el-input v-model="dataForm.diStatus" placeholder="评论状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          diId: 0,
          diBookNameId: '',
          diContent: '',
          diParent: '',
          diCreationTime: '',
          diUpdateTime: '',
          diStatus: ''
        },
        dataRule: {
          diBookNameId: [
            { required: true, message: '小说id不能为空', trigger: 'blur' }
          ],
          diContent: [
            { required: true, message: '评论内容不能为空', trigger: 'blur' }
          ],
          diParent: [
            { required: true, message: '父级评论不能为空', trigger: 'blur' }
          ],
          diCreationTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          diUpdateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          diStatus: [
            { required: true, message: '评论状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.diId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.diId) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbbookdiscuss/info/${this.dataForm.diId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.diBookNameId = data.tbBookDiscuss.diBookNameId
                this.dataForm.diContent = data.tbBookDiscuss.diContent
                this.dataForm.diParent = data.tbBookDiscuss.diParent
                this.dataForm.diCreationTime = data.tbBookDiscuss.diCreationTime
                this.dataForm.diUpdateTime = data.tbBookDiscuss.diUpdateTime
                this.dataForm.diStatus = data.tbBookDiscuss.diStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbbookdiscuss/${!this.dataForm.diId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'diId': this.dataForm.diId || undefined,
                'diBookNameId': this.dataForm.diBookNameId,
                'diContent': this.dataForm.diContent,
                'diParent': this.dataForm.diParent,
                'diCreationTime': this.dataForm.diCreationTime,
                'diUpdateTime': this.dataForm.diUpdateTime,
                'diStatus': this.dataForm.diStatus
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
